package com.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class usercontroller {
    @RequestMapping("/fileupload3")
    public String test3(HttpServletRequest request,MultipartFile upload) throws Exception {
        System.out.println("跨服务器文件上传。。。");
        /*定义上传文件服务器的路径
             这里的/upload必须实现创建好。否则报409的错误*/
        String realPath="http://localhost:9090/uploads/";

        //文件名 注意tomcat默认使用iso字符集来处理请求 中文会出翔乱码且出现500错误：返回了400错误请求的响应状态
        String filename = upload.getOriginalFilename();
        byte[] bytes=filename.getBytes("ISO-8859-1");
        filename=new String(bytes, "UTF-8");
        //使用随机数防止文件名的重复
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename=uuid+filename;
        /*
           使用fileupload服务器上传文件*/
        //1、创建客服端对象
        Client client=Client.create();
        //2、和图片服务器进行连接
        WebResource webResource=client.resource(realPath+filename);
        System.out.println(realPath+filename);
        //3、上传文件
        webResource.put(upload.getBytes());
        return "success";
    }

    @RequestMapping("/fileupload2")
    public String test2(HttpServletRequest request,MultipartFile upload) throws Exception {
        System.out.println("文件上传。。。");
        /*
        上传的位置*/
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
        //判断路径是否存在
        File file=new File(realPath);
        if (!file.exists()){
            file.mkdir();
        }
                /*
                上传文件*/
                //文件名
                String filename = upload.getOriginalFilename();
                //使用随机数防止文件名的重复
                String uuid = UUID.randomUUID().toString().replace("-", "");
                filename=uuid+filename;
                //完成上传文件
                upload.transferTo(new File(realPath,filename));
                //删除临时文件，配置文件解析器会自动帮你做

        return "success";
    }

    /*传统方式上传文件*/
    @RequestMapping("/fileupload1")
    public String test(HttpServletRequest request) throws Exception {
        System.out.println("文件上传。。。");
        //前提是使用fileupload组件（jar包）来实现上传

        /*
        上传的位置*/
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
        //判断路径是否存在
        File file=new File(realPath);
        if (!file.exists()){
            file.mkdir();
        }
        /*
        * 解析request对象，获取文件上传项*/
        //创建磁盘文件项工厂
        DiskFileItemFactory factory=new DiskFileItemFactory();
        //通过工厂获取磁盘文件项对象
        ServletFileUpload upload=new ServletFileUpload(factory);
        //解析request对象
        List<FileItem> items = upload.parseRequest(request);
        //遍历
        for (FileItem item:items) {
            if (item.isFormField()){
                //说明是普通表单项
            }else{
                /*
                上传文件*/
                //文件名
                String filename = item.getName();
                //使用随机数防止文件名的重复
                String uuid = UUID.randomUUID().toString().replace("-", "");
                filename=uuid+filename;
                //完成上传文件
                item.write(new File(realPath,filename));
                //删除临时文件
                item.delete();
            }
        }

        return "success";
    }
}
